// 2013-10-6上午11:35:38  net.betou.core.service.post.PostKeyVal.java

package net.betou.core.service.post;

import net.betou.common.util.DataConvertUtils;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.main.manager.agencyorproxy.AgencyMng;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.Connection.KeyVal;
import org.jsoup.safety.Whitelist;

/**
 * @author 叶装装
 */
public abstract class PostKeyVal  implements KeyVal{

	public static final String DEFAULT_POST_KEY = "paramStr";
	public static final String DEFAULT_SEP = "ф";
	
	private String requestKey;
	
	public PostKeyVal( ){
		this.requestKey = DEFAULT_POST_KEY;
	}
	
	public PostKeyVal( AgencyMng mng ){
		this.mng = mng;
	}
	
	public PostKeyVal( String requestKey ){
		this.requestKey = requestKey;
	}
	
	public  String key( ){
		return requestKey;
	}

	public KeyVal key( String arg0 ) {
		return null;
	}

	public  String value( ){
		return processValue( );
	}
	
	public abstract String processValue( );

	public KeyVal value( String arg0 ) {
		return null;
	}
	
	public void appendToBuffer( StringBuffer buffer, Object value ){
		buffer.append( value );
		buffer.append( DEFAULT_SEP );
	}
	
	public void appendToBufferWithDefaultSep( StringBuffer buffer, Object value, boolean appendSep ){
		buffer.append( value );
		if( appendSep ){
			buffer.append( DEFAULT_SEP );
		}
	}
	
	public String deleteWhiteSpace( String content ){
		return StringUtils.isNotBlank( content ) ? StringUtils.deleteWhitespace( content ) : content;
	}
	
	public String cleanHtml( String content ){
		if( StringUtils.isBlank( content  )){
			return content;
		}
		return Jsoup.clean( content, Whitelist.none( ) );
	}
	
	public String subString( String content ){
		if( StringUtils.isBlank( content  )){
			return content;
		}
		
		if( content.length( ) > 100 ){
			return content.substring( 0, 100 );
		}
		return content;
	}
	
	public Integer [ ] getAgencyProxy( AgentAndAdmins admin ) {

		Integer [ ] ids = new Integer [ 2 ];

		String topIntermedia = admin.getTopIntermedia( );

		// 如果发布的是机构， 0为机构的ID，1为0（代理商）
		if( admin.getAgency( ) || "0".equals( topIntermedia ) ){
			ids[ 0 ] = admin.getId( );
			ids[ 1 ] = 0;
			
			// 如果是代理商, 0要查询他上面的ID，1为自己的ID	
		}else if( admin.getProxy( ) ){
			ids [ 0 ] = getId( topIntermedia, true );
			ids [ 1 ] = admin.getId( ); 
			// 否则的话，就查询
		}else{
			ids [ 0 ] = getId( topIntermedia, true );
			ids [ 1 ] = getId( topIntermedia, false );
		}

		return ids;
	}
	
	/**
	 * @param topIntermedia
	 * @param agency	获取机构还是代理商的
	 * @return
	 */
	public Integer getId( String topIntermedia, boolean agency ){
		String [ ] tops = StringUtils.split( topIntermedia, "," );

		for ( int i = tops.length - 1; i >= 0; i-- ){
			AgentAndAdmins bean = mng.findById( DataConvertUtils.getInt( tops[ i ] ) );
			// 获取机构的
			if( agency ){
				if( bean.getAgency( ) ){
					return bean.getId( );
				}
			}else{
				
				if( bean.getProxy( ) ){
					return bean.getId( );
				}
				
			}
		}
		
		return 0;
		
		
	}
	
	private AgencyMng mng;
	
}
